package caidan;

import caidan.data.FoodAssorter;
import caidan.data.FoodData;
import jinyilw.common.TextTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.util.Map;

public class OneDayPanel extends JPanel
{
	private static final long serialVersionUID = -5314967417779676941L;
	private final OneMealPanel[] meals = new OneMealPanel[3];
	private final JLabel dayLabel = new JLabel();
	private final OneWeekView oneWeekView;
	int dayIndex;
	String dayStr;
	PositionConfiger positionConfiger;
	private String saveStr;
	Map<String, String> simpleExcelData, customExcelData;
	private final FoodAssorter assorter = new FoodAssorter();

	public OneDayPanel(int d, OneWeekView oneWeekView)
	{
		dayIndex = d;
		this.oneWeekView = oneWeekView;
		positionConfiger = oneWeekView.positionConfiger;
		simpleExcelData = oneWeekView.simpleExcelData;
		customExcelData = oneWeekView.customExcelData;
		setLayout(null);
		setBorder(RBorderFactory.getDefaultBorder());
		dayStr = MainApp.dayStrings[d];
		for (int i = 0; i < meals.length; i++)
		{
			meals[i] = new OneMealPanel(i, this);
			add(meals[i]);
		}
		// dayLabel.setForeground(Color.YELLOW);
		dayLabel.setFont(FontTools.getCustomFont(18));
		dayLabel.setHorizontalAlignment(SwingConstants.CENTER);
		dayLabel.setText(dayStr);
		add(dayLabel);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		super.setBounds(x, y, width, height);
		if (positionConfiger == null)
			return;
		int h = positionConfiger.getH() + positionConfiger.getS();
		for (int i = 0; i < meals.length; i++)
			meals[i].setBounds(0, 30 + h * i, width, h);
		dayLabel.setBounds(width / 2 - 35, 3, 70, 25);
	}

	public void assortFood()
	{
		assorter.assortFood(dayIndex);
		for (int m = 0; m < 3; m++)
		{
			meals[m].setData(assorter.getDatas(m));
			meals[m].updateMeal();
		}
		//		meals[0].setData(assorter.getBreakfastDatas());
		//		meals[1].setData(assorter.getLunchDatas());
		//		meals[2].setData(assorter.getDinnerDatas());
		//		meals[0].updateMeal();
		//		meals[1].updateMeal();
		//		meals[2].updateMeal();
	}

	public void assortFoodInThread()
	{
		new Thread(() ->
		{
			assortFood();
		}).start();
	}

	public OneMealPanel getMeal(int meal)
	{
		return meals[meal];
	}

	public String getSaveStr()
	{
		saveStr = dayStr + TextTools.lineString();
		for (int m = 0; m < 3; m++)
			saveStr = saveStr + meals[m].getSaveStr();
		return saveStr;
	}

	public int updateSimpleExcelData(int index)
	{
		for (int m = 0; m < 3; m++)
		{
			simpleExcelData.put(String.valueOf(index), meals[m].mainFood.toString());
			index++;
			simpleExcelData.put(String.valueOf(index), meals[m].sideFood.toString());
			index++;
		}
		return index;
	}

	public int updateCustomExcelData(int index)
	{
		FoodData[] foodDatas;
		for (int m = 0; m < 3; m++)
		{
			foodDatas = meals[m].getFoodDatas();
			for (FoodData foodData : foodDatas)
			{
				customExcelData.put(String.valueOf(index), foodData==null?"":
						foodData.getFoodName());
				index++;
			}
		}
		return index;
	}

	public OneWeekView getOneWeekView()
	{
		return oneWeekView;
	}
}
